//
//  FMDBMailManager.m
//  NursingManage
//
//  Created by Zhen Huang on 17/5/11.
//  Copyright © 2017年 huangzhen. All rights reserved.
//

#import "FMDBMailManager.h"
#import "FMDatabaseAdditions.h"
#import "HMailListModel.h"
static FMDBMailManager *manager = nil;

@implementation FMDBMailManager
//实现单例方法
+(FMDBMailManager *)sharedFMDBManeger {
    if (manager == nil) {
        manager = [[FMDBMailManager alloc]init];
    }
    return manager;
}

//准备好文件路径
- (NSString *)backFilePath {
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
    NSString *newFilePath = [documentPath stringByAppendingPathComponent:@"mailList.sqlite"];
    return newFilePath;//将新文件路径返回；
}

//创建数据库
- (void)dataBase {
    //创建数据库对象
    self.db = [FMDatabase databaseWithPath:[self backFilePath]];
    //打开数据库
    if ([self.db open]) {
        NSLog(@"数据库打开成功");
    }else {
        NSLog(@"数据库打开失败");
    }
}

//创建表
- (void)createTable {
    [self.db open];
    //1.准备sql语句
    NSString *sqlString = @"create table if not exists 'mailList'('emp_id'text,'empt_name'text,'mobile_phone'text,'jub_num'text,'address'text,'dept_name'text,'input_code1'text,'detp_id1'text)";
    //2.执行语句
    BOOL result = [self.db executeUpdate:sqlString];
    NSLog(result ? @"创建成功":@"创建失败");
    
    [self.db close];
    
}

//添加数据
- (void)insertDog:(HMailListModel *)MailModel {
    [self.db open];
    BOOL result = [self.db executeUpdate:@"insert into  mailList (emp_id,empt_name,mobile_phone,jub_num,address,dept_name,input_code1,detp_id1)values (?,?,?,?,?,?,?,?)",MailModel.EMP_ID,MailModel.EMP_NAME,MailModel.MOBILE_PHONE,MailModel.JUB_NUM,MailModel.ADDRESS,MailModel.DEPT_NAME,MailModel.INPUT_CODE1,MailModel.DEPT_ID1];

    NSLog(result ? @"添加成功" :@"添加失败");
    
    [self.db close];
}

//查询数据 相当于数据读取操作
- (NSMutableArray *)allModel {
    //执行查询语句，得到查询结果
    FMResultSet *set = [self.db executeQuery:@"select * from mailList"];
    //上述方法就是将数据去全部获取放进集合
    NSMutableArray *array = [NSMutableArray array];
    while ([set next]) {
        HMailListModel *model = [[HMailListModel alloc]init];
        model.EMP_NAME = [set stringForColumn:@"empt_name"];
        model.JUB_NUM = [set stringForColumn:@"jub_num"];
        model.INPUT_CODE1 = [set stringForColumn:@"input_code1"];
        model.DEPT_NAME = [set stringForColumn:@"dept_name"];
        model.ADDRESS = [set stringForColumn:@"address"];
        model.EMP_ID = [set stringForColumn:@"emp_id"];
        model.MOBILE_PHONE = [set stringForColumn:@"mobile_phone"];
        model.DEPT_ID1 = [set stringForColumn:@"detp_id1"];
        [array addObject:model];
        
    }
    [self.db close];
    return array;
}


//删除数据
- (void)deleteDog:(HMailListModel *)MailModel {
    [self.db open];
    BOOL result = [self.db executeUpdate:@"delete from MailList"];
    NSLog(result ? @"删除成功":@"删除失败");
    [self.db close];
}

////更新数据
//- (void)updateDog:(HMailListModel *)MailModel {
//    [self.db open];
//    //1.准备sql语句
//    
//    BOOL result = [self.db executeUpdate:@"update mailList set emp_id = ?,jub_num = ?,input_code1 = ?,where number = ?",aDog.name,aDog.gender,aDog.type,aDog.number];
//    NSLog(result ? @"更新成功":@"更新失败");
//    [self.db close];
//    
//}
//

@end
